import { createSlice } from '@reduxjs/toolkit'
import NetworkList from "../../utils/NetworkList"
import AsyncStorage from '@react-native-async-storage/async-storage';
const initialState = {
    NetworkList,
    Network: {
        chainId: '56',
        nickname: 'BNB Smart Chain',
        rpcUrl: 'https://bsc-dataseed4.defibit.io',
        ticker: 'BNB',
        warning: true,
        rpcPrefs: {
            blockExplorerUrl: 'https://bscscan.com',
            imageUrl: 'BNB',
            //   imageSource: require('../../images/binance.png'),
        },
    },
}

export const networkSlice = createSlice({
    name: 'network',
    initialState,
    reducers: {
        SetNetwork: (state, action) => {
            state.Network = action.payload || NetworkList[0]
        },
    },
})

export const { SetNetwork } = networkSlice.actions

export const asyncSetNetworks = (network) => async (dispatch) => {
    await AsyncStorage.setItem(
        'Network',
        JSON.stringify([network])
    );
    dispatch(SetNetwork(network));
};

export default networkSlice.reducer